package cn.pegasus.teamwork.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import cn.iyizhan.teamwork.base.util.file.FtpUtils;

public class PlistUtil {
	public static String writeToFile(Map<String, String> map,HttpServletRequest request) {
		String filesUrl = map.get("files_url");
		String packageName = map.get("app_package_name");
		String versionName = map.get("app_version");			//主版本
		String appVersionCode = map.get("app_version_code");	//子版本
		String url = "";
		//String scheme = "https://api.aizichan.cn/ios_plist/";
		BufferedWriter bufferedWriter = null;
		
		try {
			long timemillis = System.currentTimeMillis();
			String fileName = "iZichanSalary.plist";
			String path = request.getSession().getServletContext().getRealPath("/");
            File dir = new File(path+"/"+timemillis+"/");
            if (!dir.exists()) {// 判断文件目录是否存在
                dir.mkdirs();
            }
			String localFileName = path+timemillis+"/" + fileName;
			bufferedWriter = new BufferedWriter(new FileWriter(localFileName));
			String xmlStr = getXML(versionName,filesUrl,packageName,appVersionCode);
			bufferedWriter.write(xmlStr);
			bufferedWriter.flush();
			bufferedWriter.close();
//			File file = new File(localFileName);
			InputStream inStream=new FileInputStream(new File(localFileName));
			String fileUrl = FtpUtils.uploadFile(UUID.randomUUID().toString()+".plist", inStream);
	    	url = fileUrl;
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			//Close the BufferedWriter
			try {
				if (bufferedWriter != null) {
					bufferedWriter.flush();
					bufferedWriter.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return url;
	}
	
	
	public static String getXML(String versionName,String filesUrl,String packageName,String appVersionCode){
		StringBuilder plistStr = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        plistStr.append("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
        plistStr.append("<plist version=\""+versionName+"\">");
        plistStr.append("<dict>");
        plistStr.append("<key>items</key>");
        plistStr.append("<array>");
        plistStr.append("<dict>");
        plistStr.append("<key>assets</key>");
        plistStr.append("<array>");
        plistStr.append("<dict>");
        plistStr.append("<key>kind</key>");
        plistStr.append("<string>software-package</string>");
        plistStr.append("<key>url</key>");
        plistStr.append("<string>" + filesUrl + "</string>");
        plistStr.append("</dict>");
        plistStr.append("</array>");
        plistStr.append("<key>metadata</key>");
        plistStr.append("<dict>");
        plistStr.append("<key>bundle-identifier</key>");
        plistStr.append("<string>" + packageName + "</string>");
        plistStr.append("<key>bundle-version</key>");
        plistStr.append("<string>" + appVersionCode + "</string>");
        plistStr.append("<key>kind</key>");
        plistStr.append("<string>software</string>");
        plistStr.append("<key>title</key>");
        plistStr.append("<string>iZichanSalary</string>");
        plistStr.append("</dict>");
        plistStr.append("</dict>");
        plistStr.append("</array>");
        plistStr.append("</dict>");
        plistStr.append("</plist>");
        return plistStr.toString();
	}
}
